import { reqNew,reqSeckill,} from "../../request/api";


// /* 状态 */
const initState = {
    newList: [],
    seckill:[],
}

const TYPES = {
    //     /* 修改arr的type */
    XRZX_CHANGE_ARR: "XRZX_CHANGE_ARR",
    CHANGE_SECKILL:"CHAENG_SECKILL",
}

const reducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.XRZX_CHANGE_ARR:
            return {
                ...state,
                newList: action.arr,
            }
        case TYPES.CHANGE_SECKILL:
            return {
                ...state,
                seckill:action.list,
            }
       
            
        default:
            return state
    }

}

export const actions = {
    //改数据
    changeList: arr => ({ type: TYPES.XRZX_CHANGE_ARR, arr }),
    changeSeckill:list=>({type:TYPES.CHANGE_SECKILL,list}),





    /* 请求数据 */
    reqNew: () => (dispatch, getState) => {
        let { newList } = getState().home
        if (newList.length > 0) return;
        reqNew().then((res) => {
            dispatch(actions.changeList(res.data.list))

        })
    },
    reqSeckill:()=>(dispatch,getState)=>{
        let{seckill}=getState().home
        if(seckill.length>0) return;
        reqSeckill().then(res=>{            
                dispatch(actions.changeSeckill(res.data.list))            
        })
    }
}

//selecter导出数据
export const getArr = state => state.home.newList
export const getSeckill =state=>state.home.seckill

export default reducer;